<template>
  <!-- 设计图纸管理 -->
  <base-breadcrumb class="allPlan">
    <a-row :gutter="10" class="page-height">
      <a-col :span="4" style="border-right: 1px solid #d9d9d9">
        <a-card :bordered="false" class="page-height-tree">
          <title-name title="项目列表"></title-name>
          <!-- 公司组织树搜索 -->
          <PermissionTreeOrganization :collapsed="false" @select="handleSelect" />
          <!-- <tree-project
                        ref="treeOrganization"
                        @loader="search"
                        :selectedKeys.sync="selectedKeys"
                        :selectOptions.sync="selectOptions"
                    ></tree-project> -->
        </a-card>
      </a-col>
      <a-col :span="20" class="page-height-list-wrapper">
        <searchData :searchData="searchArr" @search="search"></searchData>
        <a-card class="page-height-list">
          <!-- <title-name title="图纸列表"></title-name> -->
          <!-- 表格 -->
          <list-table
            ref="table"
            :columns="columns"
            :startIndex="1"
            :request="loadData"
            :outSelectedRowKeys.sync="selectedKeys"
            :exportConfig="{ use: true, select: $route.meta.title, all: $route.meta.title, customColumns }"
            :statisticsConfig="{ use: true, statistics, temp }"
          >
            <!-- 新增 -->
            <a-button
              v-btnPermission="'tm_019_add_01'"
              slot="prefix"
              class="mb12"
              icon="plus"
              type="primary"
              @click="handleManage(2)"
              :disabled="selectOptions.tageCode !== 'PD'"
              >新增</a-button
            >
            <a-button
                    v-btnPermission="'tm_020_add_01'"
                    slot="prefix"
                    class="mb12"
                    icon="download"
                    type="primary"
                    :disabled="!selectedKeys.length"
                    @click="batchExportFiles"
            >批量导出附件</a-button>
            <span slot="operation" slot-scope="text, record">
              <MoreButton>
              <a v-btnPermission="'tm_019_loo_04'" @click="handleManage(0, record)">查看</a>
              <!-- 编辑 -->
              <template v-if="record.status == 'DRAFT' || record.status == 'REJECTED'" >
                <a @click="handleManage(1, record)" v-btnPermission="'tm_019_edi_02'">编辑</a>
              </template>
              <!-- 作废 -->
              <template v-if="record.status == 'PAST'" >
                <a @click="handleInvalid({ id: record.id, type: 2 })" v-btnPermission="'tm_019_tov_05'">作废</a>
              </template>
              <!-- 删除 -->
              <template v-if="record.status == 'DRAFT' || record.status == 'REJECTED'" >
                <a @click="handleRemoveParams({ id: record.id, type: 1 })" v-btnPermission="'tm_019_del_03'">删除</a>
              </template>
              <template v-if="record.status === 'PAST'" placement="bottom" trigger="hover">
                <template>
                  <template
                    v-if="
                      record.status === 'PAST' ||
                        record.status === 'SUBMITTED' ||
                        record.status === 'REJECTED' ||
                        record.status === 'ABOLISH'
                    "
                  >
                    <approval-button :id="record.id" :status="record.auditStatus"  v-btnPermission="'tm_019_loo_04'" />
                  </template>
                  <template v-if="record.status === 'PAST'">
                    <a v-btnPermission="'tm_019_pco_06'" @click="handleManage(-1, record)">评论</a>
                  </template>
                </template>
                <a @click="lookDetail(record, 1)" v-if="record.isChange&&record.status == 'PAST'">变更详情</a>
                <a @click="lookDetail(record, 2)" v-if="record.isReviewer&&record.status == 'PAST'">会审洽商详情</a>
                <!-- <a-icon style="font-size: 12px" type="down" /> -->
              </template>
              <template v-else>
                <template
                  v-if="
                    record.status === 'PAST' ||
                      record.status === 'SUBMITTED' ||
                      record.status === 'REJECTED' ||
                      record.status === 'ABOLISH'
                  "
                >
                  <approval-button :id="record.id" :status="record.auditStatus"  v-btnPermission="'tm_019_loo_04'" />
                </template>
                <template v-if="record.status === 'PAST'">
                  <a v-btnPermission="'tm_019_pco_06'" @click="handleManage(-1, record)">评论</a>
                </template>
                <a @click="lookDetail(record, 1)" v-if="record.isChange&&record.status == 'PAST'">变更详情</a>
                <a @click="lookDetail(record, 2)" v-if="record.isReviewer&&record.status == 'PAST'">会审洽商详情</a>
              </template>
                </MoreButton>
            </span>

            <span slot="status" slot-scope="text, record">
              <list-status :status="record.status" :isApproval="true" conversion="en" :text="text"></list-status>
            </span>
          </list-table>
        </a-card>
      </a-col>
    </a-row>
  </base-breadcrumb>
</template>

<script>
import BasePage from '@/views/basePage' // 基础配置
import { getDrawDesignList, delDrawDesign, getStatistical } from '@/api/technical/designFM'
import { batchdesignExportFiles } from "@/api/technology"
import TreeProject from '@/components/businessComponents/TreeProject' // 树
import Selector from '@/components/Select/Selector'
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '所属分公司',
    dataIndex: 'branchComName',
    ellipsis: true
  },
  {
    title: '所属项目',
    width: 300,
    dataIndex: 'projectName',
    ellipsis: true
  },
  {
    title: '专业类别',
    dataIndex: 'majorTypeName',
    ellipsis: true
  },
  {
    title: '文件名称',
    dataIndex: 'name',
    ellipsis: true
  },
  {
    title: '工程部位',
    dataIndex: 'projectListName',
    ellipsis: true
  },
  {
    title: '文件编号',
    dataIndex: 'num'
  },
  {
    title: '图纸类型',
    dataIndex: 'drawTypeName',
    ellipsis: true
  },
  {
    title: '版本号',
    dataIndex: 'drawVersion',
    ellipsis: true
  },
  {
    title: '发文部门',
    dataIndex: 'sendDepartment',
    ellipsis: true
  },
  {
    title: '收文日期',
    dataIndex: 'receiptDate',
    ellipsis: true
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
    width: '230px',
    scopedSlots: { customRender: 'operation' }
  }
]
const searchArr = [
  {
    name: '专业类别',
    type: 'tree-select',
    value: 'majorTypeId',
    attrs: {
      replaceFields: {children:'children', title:'majorName', key:'id', value: 'id' },
      showSearch: true,
      treeNodeFilterProp: 'title'
    },
    data: []
  },
  {
    name: '文件名称',
    type: 'input',
    value: 'name'
  },
  {
    name: '工程部位',
    type: 'input',
    value: 'projectListName'
  },
  {
    name: '文件编号',
    type: 'input',
    value: 'num'
  },
  {
    name: '图纸类型',
    type: Selector,
    valueKey: 'drawTypeId',
    selectType: '10',
    urlType: 'technical'
  },
  {
    name: '版本号',
    type: 'input',
    value: 'drawVersion'
  },
  {
    name: '发文部门',
    type: 'input',
    value: 'sendDepartment'
  },
  {
    name: '收文时间',
    type: 'dateRange',
    value: 'callNameDate',
    attr: {
      rangeFormat: ['receiptBeginDate', 'receiptEndDate']
    }
  }
]
import { STable } from '@/components'
import { batchExportFiles, majorTypeSelectListTree, queryProjectListByTree } from '@/api/technology'
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    STable,
    TreeProject,
    PermissionTreeOrganization
  },
  data() {
    this.columns = columns
    return {
      searchArr,
      // 统计
      statistics: {
        bluePicture: 0,
        deepPicture: 0,
        otherInteger: 0,
        totalPicture: 0,
        whitePicture: 0
      },
      temp: () => {
        return (
          <span style="margin-right: 12px">
            收到图纸：{this.statistics.totalPicture}&nbsp;份&nbsp; 施工蓝图：{this.statistics.bluePicture}
            &nbsp;份&nbsp; 深化设计图：{this.statistics.deepPicture}&nbsp;份&nbsp; 白图：
            {this.statistics.whitePicture}
            &nbsp;份&nbsp; 其它：{this.statistics.otherInteger}&nbsp;份&nbsp;
          </span>
        )
      },
      selectedKeys: [],
      selectOptions: {},
      queryParam: {},
      removeApi: { remove: delDrawDesign, invalid: delDrawDesign, undo: delDrawDesign }, // api

      loadData: async parameter => {
        const requestParameters = Object.assign({}, parameter, { query: this.queryParam })
        this.getStatisticalReq(requestParameters)
        return getDrawDesignList(requestParameters).then(res => {
          return res
        })
      },
      customColumns: [
        { title: '序号', dataIndex: 'index' },
        { title: '所属分公司', dataIndex: 'branchComName' },
        { title: '所属项目', dataIndex: 'projectName' },
        { title: '专业类别', dataIndex: 'majorTypeName' },
        { title: '文件名称', dataIndex: 'name' },
        { title: '工程部位', dataIndex: 'projectListName' },
        { title: '文件编号', dataIndex: 'num' },
        { title: '图纸类型', dataIndex: 'drawTypeName' },
        { title: '版本号', dataIndex: 'drawVersion' },
        { title: '发文部门', dataIndex: 'sendDepartment' },
        { title: '收文日期', dataIndex: 'receiptDate' },
        { title: '状态', dataIndex: 'statusStr' }
      ]
    }
  },
  mounted() {
    // this.$refs.treeOrganization.getList().then((res) => {
    //     // this.search()
    // })
    this.majorTypeSelectListTree()
  },
  methods: {
    batchExportFiles() {
      const data = {
        ids:this.selectedKeys,
        typeId: 0,
        urlStr: window.location.origin
      }
      batchdesignExportFiles(data).then(res=>{
        this.handleDownload(res)
      })
    },
    /**
     * 转义下载
     * @param parameter
     */
    handleDownload(res) {
      const blob = new Blob([res.data])
      const filename = decodeURIComponent(res.headers['content-disposition'].split('filename=')[1])
      console.log('filenamefilename',filename)
      const downloadElement = document.createElement('a')
      const href = window.URL.createObjectURL(blob) // 创建下载的链接
      downloadElement.href = href
      downloadElement.download = filename.substring(filename.indexOf("''") + 1) // 获取文件名
      document.body.appendChild(downloadElement)
      downloadElement.click() //点击下载
      document.body.removeChild(downloadElement) //下载完成移除元素
      window.URL.revokeObjectURL(href) //释放blob对象
    },
    majorTypeSelectListTree() {
      majorTypeSelectListTree().then(res=>{
        res.data.forEach(item=>{
          item.majorName = item.programmeName
          item.disabled = true
        })
        this.searchArr[0].data = res.data
      })
    },
    lookDetail(row, type) {

      if(type==2) {
        sessionStorage.setItem('talksVOS', JSON.stringify(row.talksVOS))
        this.$router.push({
          path: '/technical/designFM/jointTrial',
          query: {...row}
        })
      } else {
        sessionStorage.setItem('changeVOS', JSON.stringify(row.changeVOS))
        this.$router.push({
          path: '/technical/designFM/designDrawingChangeDetails',
          query: {...row}
        })
      }
    },
    getStatisticalReq(requestParameters) {
      getStatistical(requestParameters).then(res => {
        this.statistics = res.data
      })
    },

    handleSelect(node) {
      this.selectOptions = node
      this.search()
    },

    //搜索
    search(value) {
       value && (this.queryParam = value)
      console.log(this.selectOptions)
      if (this.selectOptions.tageCode == 'PD') {
        this.queryParam.projectId = this.selectOptions.projectId
      } else if (this.selectOptions.tageCode == 'BC') {
        this.queryParam.branchComId = this.selectOptions.orgId
      } else {
        delete this.queryParam.projectId
        delete this.queryParam.branchComId
      }
      this.$refs.table.refresh(true)
    },
    // 操作栏
    handleManage(type, record) {
      let query = {
        editModel: type == -1 ? 0 : type,
        comment: type == -1,
        projectId: this.selectOptions.projectId,
        branchComId: this.selectOptions.parentId
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: 'updateDrawingManagement',
        query
      })
    }
  }
}
</script>

<style lang="less" scoped>
@deep: ~'>>>';
.page-height-tree{
  @{deep} .ant-card-body{
    overflow-x: hidden;
  }
}
</style>